package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;

import com.ghck.server.model.Treatment;
import com.ghck.server.model.query.TreatmentQuery;
import com.ghck.server.utils.IntegerUtil;
/**
 * 就诊记录  <br>
 * t_treatment <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.TreatmentMapper
 */
 public class TreatmentProvider {
 
 	/**
 	* 新增 就诊记录
 	*/
 	public String insert(Treatment domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_treatment(create_time,last_modify_time");
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getHospitalName()!=null){
			sb.append(",hospital_name");
		}
	 	if(domain.getPathogen()!=null){
			sb.append(",pathogen");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
	 	if(domain.getActionTime()!=null){
			sb.append(",action_time");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getHospitalName()!=null){
			sb.append(",#{hospitalName}");
		}
	 	if(domain.getPathogen()!=null){
			sb.append(",#{pathogen}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
	 	if(domain.getActionTime()!=null){
			sb.append(",#{actionTime}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 就诊记录
 	*/
	public String update(Treatment domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_treatment set last_modify_time = now() ");
		
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getHospitalName()!=null){
			sb.append(",hospital_name=#{hospitalName}");
		}
		if(domain.getPathogen()!=null){
			sb.append(",pathogen=#{pathogen}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		if(domain.getActionTime()!=null){
			sb.append(",action_time=#{actionTime}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<Treatment>> map){
		StringBuffer sb = new StringBuffer();
		List<Treatment> treatments = map.get("list");
		sb.append("insert into t_treatment(create_time,last_modify_time,user_id,hospital_name,pathogen,in_used,action_time) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].hospitalName}"
	    		+ ",#'{'list[{0}].pathogen}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+ ",#'{'list[{0}].actionTime}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(treatments)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  treatments.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 就诊记录
 	*/
	public String findByQuery(TreatmentQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_treatment a where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getUserId()))
		{
			sb.append(" and a.user_id=#{userId}");
		}
		
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(TreatmentQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_treatment a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 就诊记录
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_treatment where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	